package com.chanceit.ssss.common;

import java.util.List;

import org.gl.UpdateManager;
import org.gl.utils.AndroidUtil;
import org.gl.utils.Tip;
import org.gl.utils.lg;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

public class ThridApp {
	/**
	 * 打开第三方程序
	 * 
	 * @param activity
	 * @param actionName
	 *            第三方程序在AndroidManifest中申明的actionNmae 
	 * @param packageName
	 *            AndroidManifest中申明的包名
	 * @param apkFileNameInAsset
	 *            位于本程序Asset目录下安装文件的文件路径
	 */
	public void openThridApp(final Activity activity, String actionName,
			String packageName, final String apkFileNameInAsset) {
		Intent intent;
		try {
			intent = new Intent();
			intent.setAction(actionName);
			activity.startActivity(intent);
		} catch (Exception e) {
			e.printStackTrace();
			if (!isExistScanApp(activity, packageName)) {
				AndroidUtil.genDialog(activity, "是否安装程序?", "目标程序没有安装,启动失败",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								UpdateManager um = new UpdateManager(activity);
								Tip.show("正在安装"+apkFileNameInAsset);
								um.updateFromAsset(apkFileNameInAsset);
							}
						});
			} else {
				Tip.show("未知的错误" + e.getMessage());
			}

		}
	}
	public void openThridAppForResult(final Activity activity, String actionName,
			String packageName, final String apkFileNameInAsset,int requestCode) {
		Intent intent;
		try {
			intent = new Intent();
			intent.setAction(actionName);
			activity.startActivityForResult(intent, requestCode);
		} catch (Exception e) {
			e.printStackTrace();
			if (!isExistScanApp(activity, packageName)) {
				AndroidUtil.genDialog(activity, "是否安装程序?", "目标程序没有安装,启动失败",
						new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog,
							int which) {
						UpdateManager um = new UpdateManager(activity);
						Tip.show("正在安装"+apkFileNameInAsset);
						um.updateFromAsset(apkFileNameInAsset);
					}
				});
			} else {
				Tip.show("未知的错误" + e.getMessage());
			}
			
		}
	}

	/**
	 * is exist the app
	 * 
	 * @return
	 */
	public boolean isExistScanApp(Activity activity, String packageName) {
		if (TextUtils.isEmpty(packageName)) {
			return false;
		}
		PackageManager pm = activity.getPackageManager();
		// 查询所有已经安装的应用程序
		List<ApplicationInfo> listAppcations = pm
				.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
		// Collections.sort(listAppcations,
		// new ApplicationInfo.DisplayNameComparator(pm));// 排序
		for (ApplicationInfo applicationInfo : listAppcations) {
			if (applicationInfo.packageName.equals(packageName)) {
				lg.i(this, applicationInfo.packageName);
				return true;
			}
		}
		return false;
	}

}
